
/** 로그인관리>로그인/로그아웃 를 호출되어지는 데이터 처리 Controller                                
 * @FileName  : LoginController.java 0.1 2014년09월12일(14:05:50)       
 * @Project     : nComz n-eip 프로젝트   
 * @Date         : 2014년09월12일(14:05:50)          
 * @작성자      : 신용천(nComz)           	  
 * @변경이력 :                         
 * @프로그램 설명 :로그인관리>로그인/로그아웃(Controller)    
 * Copyright 2012 (주)nComz Cop. All rights Reserved.                  
 */ 

package com.sunny.hp.lg; 

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.MultiMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import com.sunny.hp.common.servlet.ModelAndNCOMZView;
import com.sunny.hp.common.util.DateHelper;
import com.sunny.hp.common.util.EncryptUtil;
import com.sunny.hp.common.util.HttpRequest;
import com.sunny.hp.common.util.LocaleUtil;
import com.sunny.hp.common.util.MultiMapUtil;
import com.sunny.hp.common.util.PropMessageUtil;
import com.sunny.hp.common.util.SessionUtil;
import com.sunny.hp.common.util.UtilHelper;
import com.sunny.hp.lg.service.LoginService;
import com.sunny.hp.menu.service.MenuService;

/** 로그인관리>로그인/로그아웃 를 호출되어지는 데이터 처리 Controller                               
 * @ClassName  : LoginController       
 * @Date         : 2014년09월12일(14:05:50)         
 * @작성자      : 신용천(nComz)           	 
 * @변경이력 :                        
 * @프로그램 설명 :로그인관리>로그인/로그아웃(Controller)                     
 */      

@Controller
@RequestMapping("/lg")
public class LoginController {

	@Autowired
	private LoginService loginService; 
	
	@Autowired
	private MenuService menuService;
	
	@Autowired
	private PropMessageUtil propMessageUtil; 
	
	protected final Log logger = LogFactory.getLog(getClass());

	/**  로그인관리>로그인/로그아웃>로그인 폼 					
	 * @Method Name  : selectLoginFormView   		
	 * @작성일   : 2014년09월12일(14:05:50)                    		
	 * @작성자   : 신용천(nComz)                    		
	 * @변경이력  :                           		
	 * @Method 설명 : 로그인 폼                         		
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @return mav                                	
	 */
	@RequestMapping(value="/selectLoginFormView.do")
	public ModelAndNCOMZView selectLoginFormView(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndNCOMZView mav   	= new ModelAndNCOMZView(request);
		mav.setViewName("lg/selectLoginFormView");

		return mav;
	}


	/**  로그인관리>로그인/로그아웃>로그인 					
	 * @Method Name  : selectLogin   		
	 * @작성일   : 2014년09월12일(14:05:50)                    		
	 * @작성자   : 신용천(nComz)                    		
	 * @변경이력  :                           		
	 * @Method 설명 : 로그인                         		
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @return mav                                	
	 */
	@RequestMapping(value="/ajax/selectLogin.do")
	public ModelAndNCOMZView selectLogin(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
		ModelAndNCOMZView mav   	= new ModelAndNCOMZView(request,new MappingJacksonJsonView());
		HttpRequest requestUtil = new HttpRequest(request); 
		UtilHelper csUtil = new UtilHelper(); 
		DateHelper dateHelper = new DateHelper();
		LinkedHashMap param = requestUtil.paramBind(); 
		
		Locale locale = LocaleUtil.getLocale(request);
		String resultMsg= "";
		
		String user_password = csUtil.getString(param.get("user_password"), "");		
		user_password = EncryptUtil.encrypt(user_password);
		
		String resultCode = "200";
		String resultMessage = "";
		LinkedHashMap resultMap1 = new LinkedHashMap(); 
		try {
			resultMap1 = loginService.selectLogin(param);
			if(resultMap1 == null){
				resultCode = "100";// 입력한 아이디와 일치하는 정보가 없음.
				resultMsg = propMessageUtil.propFormat("message.parameter.userid.invalid", locale);
			}else{
				if(user_password.equals(resultMap1.get("user_password"))){
					String expired_date = csUtil.getString(resultMap1.get("expired_date"), "").replaceAll("-", "");
					boolean isLogin = true;
					if(!expired_date.equals("")){ // 계정 만료 날짜를 확인하여 만료 되었으면 로그인을 불허한다.
						if(Integer.parseInt(expired_date) < Integer.parseInt(dateHelper.getStrDate())){
							resultCode = "300";// 사용자 계정 Expired
							isLogin = false;
							resultMsg = propMessageUtil.propFormat("message.id.expired", locale);
						}
					}
					
					if(isLogin){
						resultMsg = propMessageUtil.propFormat("message.login.success", locale,  resultMap1.get("user_name"));
						session.setAttribute("userSession", resultMap1);
						
						List<LinkedHashMap> menuMap = menuService.selectMenuList(param);						
						MultiMap menuMultiMap = MultiMapUtil.getLinkedMultiMap(menuMap, "up_menu_id");
						
						session.setAttribute("userMenuMap", menuMultiMap);
						
					}
				}else{
					resultCode = "400";// 패스워드 불일치
					resultMsg = propMessageUtil.propFormat("message.parameter.password.invalid", locale);
				}
			}	
			mav.addObject("resultMsg", resultMsg);
			mav.addObject("resultCode", resultCode);
			
		} catch (Exception e) {
			e.printStackTrace(); 
		} 

		return mav;
	}


	/**  로그인관리>로그인/로그아웃>로그아웃 					
	 * @Method Name  : selectLogout   		
	 * @작성일   : 2014년09월12일(14:05:50)                    		
	 * @작성자   : 신용천(nComz)                    		
	 * @변경이력  :                           		
	 * @Method 설명 : 로그아웃                         		
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @return mav                                	
	 */
	@RequestMapping(value="/ajax/selectLogout.do")
	public ModelAndNCOMZView selectLogout(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
		ModelAndNCOMZView mav   	= new ModelAndNCOMZView(request,new MappingJacksonJsonView());
		Locale locale = LocaleUtil.getLocale(request);
		SessionUtil sessionUtil = new SessionUtil();
		String resultMsg = propMessageUtil.propFormat("message.logout.success", locale, sessionUtil.getUserSession().get("user_name"));
		session.invalidate();
		
		mav.addObject("resultMsg", resultMsg);
		
		return mav;
	}

}
